import os
import time
import win32gui
import win32api
import win32con
import pyautogui as pg


# 需要在使用前重启下ERP

def mid_click(handle): #点击控件中央
    pos = win32gui.GetWindowRect(handle) #获取位置
    x = (pos[0]+pos[2])/2
    y = (pos[1]+pos[3])/2
    print(x, y)
    pg.click(x, y, button='left')
    return x, y


def ERP_data_downloader():
    #os.system('"C:\WXFBEXE\XSGL\XSGL.exe"')
    time.sleep(5)
    erp_window = win32gui.FindWindow("TMainWin", "金力供应链系统--综合查询") #获取整个ERP的窗口句柄
    login_window = win32gui.FindWindow("TDoLogin_Form", "系统用户登录") #获取登录窗口句柄
    #print(login_window)
    win32gui.SetForegroundWindow(login_window) #把登录窗口放在最前
    panel = win32gui.FindWindowEx(login_window, None, "TPanel", None) #获取登录窗口密码
    password_input = win32gui.FindWindowEx(panel, None, "TFlatEdit", None) #获取密码输入控件句柄
    mid_click(password_input)
    time.sleep(0.5)
    pg.press(['a', 'b', 'c'])
    time.sleep(0.5)
    enter_button = win32gui.FindWindowEx(login_window, None, "TBMPButton", "btnSure")
    mid_click(enter_button)
    return

ERP_data_downloader()